package cn.lkx.serviceucenter.service.impl;

import cn.lkx.commonutils.JwtUtils;
import cn.lkx.commonutils.MD5;
import cn.lkx.servicebase.exceptionhandle.MyException;
import cn.lkx.serviceucenter.entity.UcenterMember;
import cn.lkx.serviceucenter.entity.vo.RegisterVo;
import cn.lkx.serviceucenter.mapper.UcenterMemberMapper;
import cn.lkx.serviceucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    //会员登录 通过手机号或qq邮箱 和密码
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile(); //传过来的手机号/邮箱号
        String password = member.getPassword();//传过来的密码
        //校验参数是否空值
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登录失败");
        }
        //通过邮箱查询到会员
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        if (null == ucenterMember){
            throw new MyException(20001,"登录失败");
        }
        //校验密码
        if ( !MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new MyException(20001,"登录失败");
        }
        //校验该会员是否被拉黑名单
        if (ucenterMember.getIsDisabled()){
            throw new MyException(20001,"该账户已被封禁");
        }
        //校验全部通过后，使用JWT生成token字符串返回
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }
    //会员注册方法
    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();  //可当邮箱可当手机号
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //非空校验
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code) ){
            throw new MyException(20001,"注册失败");
        }
        //查询该手机号或 邮箱是否已注册
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count > 0){
            throw new MyException(20001,"该手机号/邮箱已被注册");
        }
        //校验验证码，从redis获取验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if ( !code.equals(redisCode)){
            throw new MyException(20001,"验证码错误");
        }
        //校验全部通过后，保存数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(registerVo.getNickname()); //昵称
        ucenterMember.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/lWfF1X3ty2ZYr02nhibB7iaBGE6lUcibrT7RLkrhbYlYY9zb6TFvMRrIWL8kJ47vuYjxqSaEc4Ctva6nDYaibNXd9A/132");//默认头像
        int insert = baseMapper.insert(ucenterMember);
        return insert>0;
    }

    //根据openid查询数据库是否存在该用户 （微信登录用，扫码先判断是否已有该openId，在保存用户到数据库）
    @Override
    public UcenterMember getMemberByOpenId(String open_id) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",open_id);
        return baseMapper.selectOne(queryWrapper);
    }

    //获取某一天的注册人数, 给statistics 模块统计调用
    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}
